package com.marvels.financial.collection.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.PageInfoDTO;
import com.marvels.common.utils.DateUtils;
import com.marvels.common.utils.StringUtils;
import com.marvels.financial.collection.dao.LoanAfteManageDao;
import com.marvels.financial.collection.dto.CollectListConditionDTO;
import com.marvels.financial.collection.dto.LoanAfteManageDTO;
import com.marvels.financial.collection.dto.ReminderDTO;
import com.marvels.financial.collection.dto.loanAfterDownDTO;
import com.marvels.financial.collection.service.LoanAfteManageService;
import com.marvels.financial.consum.FinancialConsumerProductApi;
import com.marvels.financial.loan.dto.McptProductType;

@Service
public class LoanAfteManageImpl implements LoanAfteManageService{

	@Autowired
	private LoanAfteManageDao loanAfteManageDao;
	
	@Autowired
	private FinancialConsumerProductApi consumerProductApi;
	@Override
	public List<LoanAfteManageDTO> loanAfterList(CollectListConditionDTO condition) {
		List<LoanAfteManageDTO> manageDTOList=loanAfteManageDao.loanAfterList();
		if(manageDTOList != null && manageDTOList.size() > 0){
			for(LoanAfteManageDTO loanAfteManage : manageDTOList){
				String productTypeId = loanAfteManage.getProductType();
				if(StringUtils.isNotEmpty(productTypeId)){
					McptProductType productType=new McptProductType();
					productType.setProductType(productTypeId);
					PageInfoDTO<McptProductType> productPage=consumerProductApi.productTypePageInfo(productType);
					if(productPage != null){
						List<McptProductType>  list = productPage.getList();
						if(list !=null && list.size()>0){
							McptProductType mcptProductType = list.get(0);
							loanAfteManage.setProductTypeName(mcptProductType.getProductTypeName());
						}
						
					}
				}
				String productTypeName = loanAfteManage.getProductTypeName();
				if(StringUtils.isEmpty(productTypeName)){
					loanAfteManage.setProductTypeName(productTypeId);
				}
			}
		}
		return manageDTOList;
	}
	

	@Override
	public HSSFWorkbook downLoadExcel(CollectListConditionDTO condition) {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("firstSheet");
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setDefaultColumnWidth(10);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = workbook.createCellStyle();
		// 居中格式、加粗
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		org.apache.poi.hssf.usermodel.HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setBorderBottom((short) 1);// 下边框
		style.setBorderLeft((short) 1);// 左边框
		style.setBorderRight((short) 1);// 右边框
		style.setBorderTop((short) 1);// 上边框
		style.setLocked(true);
		style.setFont(font);
		// 设置表内容格式
		HSSFCellStyle style2 = workbook.createCellStyle();
		style2.setBorderBottom((short) 1);// 下边框
		style2.setBorderLeft((short) 1);// 左边框
		style2.setBorderRight((short) 1);// 右边框
		style2.setBorderTop((short) 1);// 上边框
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("身份证");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("手机号");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("产品名称");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("贷款时间");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("合同金额");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("贷款余额");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("还款银行卡号");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("还款银行名称");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("当前应还期数");
		cell.setCellStyle(style);
		cell = row.createCell(11);
		cell.setCellValue("逾期天数");
		cell.setCellStyle(style);
		cell = row.createCell(12);
		List<LoanAfteManageDTO> manageDTOList=loanAfteManageDao.loanAfterList();
		List<loanAfterDownDTO> list = loanAfteManageDao.loanAfterDownList(condition.getProductType());
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				loanAfterDownDTO dto = list.get(i);
				row = sheet.createRow((int) i + 1);
				// 创建单元格，并设置值
				row.createCell(0).setCellValue(String.valueOf(i + 1));
				row.createCell(1).setCellValue(dto.getCustName());
				row.createCell(2).setCellValue(dto.getCustCardNo() + "");
				row.createCell(3).setCellValue(dto.getMobileNo());
				String productTypeName = dto.getProductTypeName();
				if(StringUtils.isEmpty(productTypeName)){
					String productTypeId = dto.getProductType();
					if(StringUtils.isNotEmpty(productTypeId)){
						McptProductType productType=new McptProductType();
						productType.setProductType(productTypeId);
						PageInfoDTO<McptProductType> productPage=consumerProductApi.productTypePageInfo(productType);
						if(productPage != null){
							List<McptProductType>  listProduct = productPage.getList();
							if(listProduct !=null && listProduct.size()>0){
								McptProductType mcptProductType = listProduct.get(0);
								productTypeName = mcptProductType.getProductTypeName();
							}
							
						}
					}
					if(StringUtils.isEmpty(productTypeName)){
						productTypeName = productTypeId;
					}
				}
				row.createCell(4).setCellValue(productTypeName);
				row.createCell(5).setCellValue(DateUtils.getDateYYYY_MM_DD(dto.getLoanTime()));
				row.createCell(6).setCellValue(String.valueOf(dto.getContractAmount()));
				row.createCell(7).setCellValue(String.valueOf(dto.getLoanBalance()));
				row.createCell(8).setCellValue(dto.getPaymentAcctNo());
				row.createCell(9).setCellValue(dto.getPaymentBankName());
				row.createCell(10).setCellValue(dto.getCurrentPaymentPeriod());
				row.createCell(11).setCellValue(dto.getOverdueDays());
			}
		}
		return workbook;
	}

}
